package com.hanhy.util;


import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

import com.hanhy.util.Locator.ByType;

/**
 * Created by Administrator on 2016/12/12.
 */
public class ConfigUtil {

    static Log log = new Log(ConfigUtil.class);
    public static String getConstantValue(String name) {
        String value = null;
        try {
            InputStream inputStream = ConfigUtil.class.getResourceAsStream("/constantConfig.xml");
            SAXReader saxReader = new SAXReader();
            Document dom = saxReader.read(inputStream);
            Element rootElement = dom.getRootElement();
            Iterator<Element> iterator = rootElement.elements("module").iterator();
            while (iterator.hasNext()) {
                Element moduleElement = iterator.next();
                Element nameElement = moduleElement.element("name");
                if (nameElement.getText().equals(name)) {
                    value = moduleElement.element("value").getText();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;

    }

    public static HashMap<String, Locator> readXmlUtil(String pageName) throws Exception {
//        Log log = new Log(ConfigUtil.class);
        log.info(pageName);
        HashMap<String, Locator> locatorHashMap = new HashMap<>();
        locatorHashMap.clear();

        InputStream inputStream = ConfigUtil.class.getResourceAsStream("/pageElementLocator.xml");
        SAXReader saxReader = new SAXReader();
        Document dom = saxReader.read(inputStream);
        Element rootElement = dom.getRootElement();

        for (Iterator<?> i = rootElement.elementIterator(); i.hasNext(); ) {
            Element page = (Element) i.next();
            if (page.attribute(0).getValue().equalsIgnoreCase(pageName)) {
                log.info("page info is:" + pageName);
                for (Iterator<?> l = page.elementIterator(); l.hasNext(); ) {
                    Element locator = (Element) l.next();
                    String type = null;
                    String timeOut = null;
                    String value = null;
                    String locatorName = null;

                    for (Iterator<?> j = locator.attributeIterator(); j.hasNext(); ) {
                        Attribute attribute = (Attribute) j.next();
                        if (attribute.getName().equalsIgnoreCase("type")) {
                            type = attribute.getValue();
                            log.info("get locator type:" + type);
                        }else if (attribute.getName().equalsIgnoreCase("timeOut")){
                            timeOut = attribute.getValue();
                            log.info("get locator timeOut:"+timeOut);
                        }else if (attribute.getName().equalsIgnoreCase("value")){
                            value = attribute.getValue();
                            log.info("get locator value:"+value);
                        }
                    }
                    Locator temp = new Locator(value,Integer.parseInt(timeOut),getByType(type));
                    locatorName = locator.getText();
                    log.info("locator Name is:"+locatorName);
                    locatorHashMap.put(locatorName,temp);
                }
                continue;
            }

        }
        return locatorHashMap;
    }


private static ByType getByType(String type) {
    ByType byType = ByType.xpath;
    if (type == null||type.equalsIgnoreCase("xpath")){
        byType = ByType.xpath;
    }else if (type.equalsIgnoreCase("name")){
        byType = ByType.name;
    }else if (type.equalsIgnoreCase("id")){
        byType= ByType.id;
    }else if (type.equalsIgnoreCase("linkText")){
        byType = ByType.linkText;
    }else if (type.equalsIgnoreCase("partialLinkText")){
        byType = ByType.partialLinkText;
    }else if (type.equalsIgnoreCase("cssSelector")){
        byType = ByType.cssSelector;
    }else if (type.equalsIgnoreCase("className")){
        byType = ByType.className;
    }else if (type.equalsIgnoreCase("tagName")){
        byType = ByType.tagName;
    }
    return  byType;
    }
    public static void main(String[] args){
        log.info(ConfigUtil.getConstantValue("userName")+","+ConfigUtil.getConstantValue("password"));

    }
}
